#_*_coding:utf-8_*_
from bs4 import BeautifulSoup
import urllib.request
import json

#豆瓣新片榜、一周口碑、北美口碑
def newplay():
    response = urllib.request.urlopen("https://movie.douban.com/chart");
    buff = response.read();
    html = buff.decode("utf8");
    soup = BeautifulSoup(html);
    pidsoup = BeautifulSoup(str(soup.find(attrs={"id":"content"})));

    #新片榜
    newartic=BeautifulSoup(str(pidsoup.find(attrs={"class":"indent"})));
    newlist=newartic.find_all("table");
    nd=[];
    print("新片榜：")
    for x in newlist:
        #strip()是去空格的
         title=x.find_all("a")[1].get_text().split("/")[0].strip();
         url=x.find_all("a")[1].get("href");
         nd.append(title+"  "+url);
         print(title+"  "+url);

    week=BeautifulSoup(str(pidsoup.find(attrs={"id": "listCont2"})));
    weeklist=week.find_all("a");
    wl=[];
    print("一周口碑榜：");
    for x in weeklist:
        np=x.get_text().strip()+"  "+x.get("href");
        wl.append(np);
        print(np);

    beimei = BeautifulSoup(str(pidsoup.find(attrs={"id": "listCont1"})));
    bmlist = beimei.find_all("a");
    bl = [];
    print("北美口碑榜:")
    for x in bmlist:
        bp = x.get_text().strip() + "  " + x.get("href");
        bl.append(bp);
        print(bp);
    return nd,wl,bl;

#top250榜单
def top250():
    toplist=[];
    m=0;
    url="https://movie.douban.com/top250?start=";
    while(m<11):
        fullurl=url+str(m*25);
        m=m+1;
        response = urllib.request.urlopen(fullurl);
        buff = response.read();
        html = buff.decode("utf8");
        soup = BeautifulSoup(html);
        content=BeautifulSoup(str(soup.find(attrs={"id":"content"})));
        li=BeautifulSoup(str(content.find(attrs={"class":"grid_view"})));
        list=li.find_all("li");
        for x in list:
            title=x.find_all("a")[1].get_text().split("/")[0].strip();
            murl=x.find_all("a")[1].get("href");
            toplist.append(title+" "+murl);
            print(title+" "+murl);
    return toplist;

def toptype(type,start=0,limit=20):
    typedict = {"剧情": 11, "喜剧": 24, "动作": 5, "爱情": 13, "科幻": 17, "动画": 25, "悬疑": 10, "惊悚": 19, "恐怖": 20, "纪录片": 1,
                "短片": 23, "奇幻": 16, "冒险": 15, "灾难": 12, "武侠": 29, "古装": 30, "黑色电影": 31};
    typelist=[];
    url="https://movie.douban.com/j/chart/top_list?type="+str(typedict[type])+"&interval_id=100%3A90&action=&start="+str(start)+"&limit="+str(limit);
    response = urllib.request.urlopen(url);
    move=json.load(response);
    for x in move:
        content=x["title"]+" "+str(x["score"])+" "+x["url"];
        print(content);
        typelist.append(content);
    return typelist;

toptype("灾难",0,10);

